package com.lanou.mynews.movie;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.mynews.R;

import java.util.List;


public class ContentActivity extends Activity implements View.OnClickListener {
    private NoScrollGridView gridView;
    private ContentAdapter contentAdapter;
    private List<Content.FilmListEntity.ActorInfoEntity> actorInfos;
    private RequestQueue requestQueue;
    private List<Content.FilmListEntity> data;
    private List<Content.FilmListEntity.PictureListEntity> datas;
    Handler handler;
    private ImageView imageView_big;
    private TextView textview_filmName;
    private TextView textview_grade;
    private TextView textview_english;
    private TextView textview_filmType;
    private TextView textview_length;
    private TextView textview_nation;
    private TextView textView_synopsis;
    private Button button1;
    private Button button2;

    Content content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_community);
        requestQueue = Volley.newRequestQueue(this);
        gridView = (NoScrollGridView) findViewById(R.id.gridView);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);

        imageView_big = (ImageView) findViewById(R.id.imageView_big);
        textview_filmName = (TextView) findViewById(R.id.textview_filmName);
        textview_grade = (TextView) findViewById(R.id.textview_grade);
        textview_english = (TextView) findViewById(R.id.textview_english);
        textview_filmType = (TextView) findViewById(R.id.textview_filmType);
        textview_length = (TextView) findViewById(R.id.textview_length);
        textview_nation = (TextView) findViewById(R.id.textview_nation);
        textView_synopsis = (TextView) findViewById(R.id.textView_synopsis);


        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView_big,
                        R.drawable.loading, R.drawable.loading);
                ImageLoader imageLoader = VolleySingleton.getInstance(getApplicationContext()).
                        getImageLoader();
                imageLoader.get(msg.getData().getString("postUrl"), listener);
                textview_filmName.setText(msg.getData().getString("filmName_cn"));
                textview_grade.setText(msg.getData().getString("grade_cn"));
                textview_english.setText(msg.getData().getString("englisg_cn"));
                textview_filmType.setText(msg.getData().getString("filmType_cn"));
                textview_length.setText(msg.getData().getString("length_cn"));
                textview_nation.setText(msg.getData().getString("nation_cn"));
                textView_synopsis.setText(msg.getData().getString("synopsis_cn"));

                return false;
            }
        });

        String getUrl = "http://mobileif.maizuo.com/version4/film/" + getIntent().getStringExtra("id") + "?encode=Wpn5ANf0b8FB9MJ2UHhMh0looZXDSEU%252FfM6mpEMzktHCJB%252Bd3jHdZHi65%252BrciRZG4VUDi07%252F1gjK%250APbXGlbLiWZylVbMkcibqBNz%252FNOwhNY1iBN3HgNOi%252BHMzqDudxSA6JDA6p4leZCKpl1tzdNfnlHUJ%250A9JNN30Usldz9litdTs0%253D&param=hQLnIHlFyiUGPjwGddMKO5ZGn0ivClmM0MW4snXJnAgc%252FTnHjgICr%252B%252F5Py%252BRpBkW8hEg24vfrV1P%250Az21ihgo2Zpc1iVsJEZWY3LJ2jD4nfffZhbSOzoIVycywX2tE9CNF&channelID=31&clientID=31";
        System.out.println(getUrl);
        StringRequest request = new StringRequest(getUrl,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {// 网址正确的时候会回调的方法
                        if (response != null) {// response是从网络返回的全部数据,字符串类型
                            Gson gson = new Gson();
                            // 将解析的字符串类型的值,转成TicketResult中的格式
                            content = gson.fromJson(response,
                                    Content.class);


                            if (content != null) {

                                    actorInfos = content.getFilmList().get(0).getActorInfo();
                                    datas = content.getFilmList().get(0).getPictureList();
                                    Log.d("lanou", "onResponse() called with: " + "response = [" + response + "]");

                                    Message message = new Message();
                                    Bundle bundle = new Bundle();
                                    bundle.putCharSequence("postUrl", content.getFilmList().get(0).getBigPoster());
                                    bundle.putCharSequence("filmName_cn", content.getFilmList().get(0).getFilmName());
                                    bundle.putCharSequence("grade_cn", content.getFilmList().get(0).getGrade());
                                    bundle.putCharSequence("englisg_cn", content.getFilmList().get(0).getEnglishName());
                                    bundle.putCharSequence("filmType_cn", content.getFilmList().get(0).getFilmType());
                                    bundle.putCharSequence("length_cn", content.getFilmList().get(0).getLength());
                                    bundle.putCharSequence("nation_cn", content.getFilmList().get(0).getNation());
                                    bundle.putCharSequence("synopsis_cn", content.getFilmList().get(0).getSynopsis());

                                    message.setData(bundle);
                                    handler.sendMessage(message);


                                    //绑定适配器
                                    contentAdapter = new ContentAdapter(ContentActivity.this, actorInfos);
                                    gridView.setAdapter(contentAdapter);

                                }
                            }
                        }

                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub

            }
        });
        // 将图片请求对象加入到请求队列中
        requestQueue.add(request);


    }

    @Override
    public void onClick(View v) {
        Intent intent;
        Bundle bundle;
        switch (v.getId()) {
            case R.id.button1:
                intent = new Intent(ContentActivity.this, PhotoActivity.class);
                bundle = new Bundle();
                bundle.putSerializable("imageList", content);
                intent.putExtra("tp", bundle);
                startActivity(intent);
                break;
            case R.id.button2:
                intent = new Intent(ContentActivity.this, ReviewActivity.class);
                intent.putExtra("id", content.getFilmList().get(0).getFilmID());
                startActivity(intent);
                break;

        }


    }
}
